<template>
	<view class="mean">
		<view v-for="(item,index) in list" :key="item.id">
			<text style="margin-right: 4px;">
				{{item.wordSex2}}{{parentData.languageType===1?".":""}}
			</text>
			<template v-for="(item2,index2) in item.chinExplain" :key="index2">
				<text :class="{'colorExplain':item2.isExplain}">
					{{item2.chinExplain}}{{item2.splitstr}}
				</text>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		name: "wordexplain",
		props: {
			data: Array | String,
			parentData: Object
		},
		data() {
			return {
				list: [],
				wordExplain: '',
			};
		},
		watch: {
			data() {
				this.formatData();
			}
		},
		mounted() {
			this.formatData();
		},
		methods: {
			formatData() {
				let data = this.data;
				// console.log('parentData=',this.parentData)
				if (typeof data === "string") {
					try {
						data = JSON.parse(data);
					} catch (e) {
						data = [];
					}
				}

				let wordExplainList = []
				if (this.parentData.exampleSentence.length) {
					let exampleSentence = this.parentData.exampleSentence
					exampleSentence.filter((item) => item.trueQuestion && item.wordExplains).map((item) => {
						let wordExplain = item.wordExplains[0].wordExplain
						// console.log('wordExplain=',wordExplain)
						wordExplain = wordExplain.replace(/[,。，]/g, "，");
						wordExplainList.push(wordExplain)
					})
				}
				// console.log('wordExplainList=', wordExplainList)

				if (Array.isArray(data)) {
					let keyMaps = {},
						lists = [];
					let languageType = this.parentData.languageType || 1;
					let splitstr = languageType == 1 ? "；" : "。"
					// "[{"id": 1, "wordSex": "vt.及物动词", "selected": false, "chinExplain": "（不顾责任、义务等）离弃，遗弃，抛弃"}]
					let chinExplainList = []
					data.forEach((item) => {
						if (typeof item.chinExplain === "string") {
							item.chinExplain = item.chinExplain.replace(/[,。，]/g, "，");
						}
						//得到wordSex2=vt
						if (typeof item.wordSex === "string") {
							//item.wordSex = item.wordSex.replace(/\[.*\]/g,"");
							item.wordSex2 = item.wordSex.split(/[\.\.]/g)[0];
							item.wordSex2 = item.wordSex2.trim();
							// console.log('item.wordSex2=',item.wordSex2)
						}
						//拼接相同的词性
						if (keyMaps[item.wordSex2]) {
							let obj = {
								splitstr: splitstr,
								chinExplain: item.chinExplain,
								isExplain: wordExplainList.includes(item.chinExplain),
							}
							keyMaps[item.wordSex2] = {
								wordSex2: item.wordSex2,
								chinExplain: [...keyMaps[item.wordSex2].chinExplain, obj]
							};
						} else {
							let obj = {
								splitstr: splitstr,
								chinExplain: item.chinExplain,
								isExplain: wordExplainList.includes(item.chinExplain),
							}
							keyMaps[item.wordSex2] = {
								wordSex2: item.wordSex2,
								chinExplain: [obj]
							};
						}
					});
					// console.log('keyMaps=', keyMaps)
					for (let key in keyMaps) {
						lists.push(keyMaps[key]);
					}
					this.list = lists;
					// console.log('lists=', lists)
				}
			},

		}
	}
</script>

<style lang="scss">
	.mean {
		font-size: 28rpx;
		color: #575A64;
		margin-bottom: 20rpx;
	}

	.colorExplain {
		color: #5480F8;
	}
</style>